﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace QuantumQuest
{
    public partial class QQTestLevel : QQLevel
    {
        private DispatcherTimer _timer;
        private QQPhysics _phys;
        private bool _thrustApplied;

        private double _degsRevolved;
        private QQVector2 _diffVec;
        private bool _success = false;
        private double _revolutionsToFinish = 2;

   
        private bool _hasBeenInSpace = false;


        private QQPlanetEarth _earth;
        private QQRocket _rocket;


        public QQTestLevel()
        {
            InitializeLevel();
          
           
        }

      
        
        public override void ShutDown()
        {
            QQGlobal.KeysDown.RemoveRange(0, QQGlobal.KeysDown.Count);
            if (_success)
            {
                QQGlobal.Score += (int)(_rocket.Fuel * 100.0);
            }
        }
        public override void Begin()
        {
            InitializeComponent();
           
            InitializeTestLevel();
            _timer.Start();
        }
        public void InitializeTestLevel()
        {

            _thrustApplied = false;
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(QQGlobal.FramerateSeconds);
            _timer.Tick += new EventHandler(_timer_Tick);

            _phys = new QQPhysics();


            _bodies = new QQList(2);
            
            _earth = new QQPlanetEarth(10000000000000000,new QQVector2(300, 300));
            _rocket = new QQRocket(10, 100000000, new QQVector2(370, 300));

           

            _bodies.Add(_earth);
            _bodies.Add(_rocket);

            _diffVec = new QQVector2(0, 0);
            _diffVec = _rocket.PositionVec - _earth.PositionVec;
          
      
            for (int i = 0; i < _bodies.Count; i++)
            {
                TestLevelCanvas.Children.Add((QQBody)_bodies[i]);
              
            }
        }

        
      
        void _timer_Tick(object sender, EventArgs e)
        {
            if (QQGlobal.KeysDown.Contains(Key.Up))
            {
                _thrustApplied = true;
            }


            Line redLine = new Line();
            redLine.X1 = _rocket.PositionVec.X;
            redLine.Y1 = _rocket.PositionVec.Y;
            redLine.X2 = _rocket.PositionVec.X + _rocket.ThrustVec.X*35;
            redLine.Y2 = _rocket.PositionVec.Y + _rocket.ThrustVec.Y*35;
            SolidColorBrush redBrush = new SolidColorBrush();
            redBrush.Color = Colors.Red;
            redLine.StrokeThickness = 1;
            redLine.Stroke = redBrush;
            TestLevelCanvas.Children.Add(redLine);

            FuelText.Text = Convert.ToString(_rocket.Fuel);
            RotText.Text = Convert.ToString(_rocket.ThrustVec.ThetaDeg);
            ThrustMagText.Text = Convert.ToString(_rocket.ThrustVec.Magnitude);


            if ((_earth.PositionVec - _rocket.PositionVec).Magnitude > 100)
            {
                _hasBeenInSpace = true;
            }
            


            if (_thrustApplied)
            {
                if (_hasBeenInSpace)
                {
                    if (_phys.Collide(_earth, _rocket))
                    {
                        _isFinished = true;
                    }
                   


                }
                if (Math.Abs(_degsRevolved) >= 360.0*_revolutionsToFinish)
                {
                    _isFinished = true;
                    _success = true;
                }
                QQVector2 tempVec = new QQVector2(0, 0);
                tempVec = _rocket.PositionVec - _earth.PositionVec;
                _degsRevolved += Math.Abs(_diffVec.ThetaDeg - tempVec.ThetaDeg)/2.0;
                _diffVec = _rocket.PositionVec - _earth.PositionVec;

               

                _phys.UpdateCollisions3(ref _bodies);

                for (int i = 0; i < _bodies.Count; i++)
                {

                    ((QQBody)(_bodies[i])).DynamicsParameters = (_phys.ApplyGravity2(_bodies, i, QQGlobal.Dt)).DynamicsParameters;

                    ((QQBody)(_bodies[i])).Update();


                }
            }
        }

    }
}
